package View;

import Controller.ButtonController;
import Controller.MenuController;

import javax.swing.*;
import java.awt.*;

public class GameView extends JFrame {

    //为主页面的 菜单栏 和 按钮 增加事件监听
    private final MenuController menuController;
    private final ButtonController buttonController;

    private final JMenuBar jmb;
    private final JButton[] jb;
    private final JMenu file;
    private final JMenu help;
    private final JMenuItem afresh;
    private final JMenuItem quit;
    private final JMenuItem about;

    public GameView(MenuController menuController, ButtonController buttonController){
        super("井字棋");

        jmb = new JMenuBar();
        jb = new JButton[9];
        file = new JMenu("文件");
        help = new JMenu("帮助");
        afresh = new JMenuItem("重新开始");
        quit = new JMenuItem("退出");
        about = new JMenuItem("关于");

        this.menuController = menuController;
        this.buttonController = buttonController;
        this.buttonController.setButtons(jb);

    }

    public void display(){

        setSize(500,500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setResizable(false);


        addMenu();

        addButton();

        setVisible(true);

    }

    //主页面上方菜单栏
    private void addMenu(){

        Font menufont = new Font("微软雅黑",Font.PLAIN,12);

        file.add(afresh);
        file.addSeparator();
        file.add(quit);
        jmb.add(file);

        help.add(about);
        jmb.add(help);

        file.setFont(menufont);
        help.setFont(menufont);
        about.setFont(menufont);
        quit.setFont(menufont);
        about.setFont(menufont);

        afresh.addActionListener(e->menuController.AfreshController());
        quit.addActionListener(e->menuController.QuitController());
        about.addActionListener(e->menuController.AboutController());

        setJMenuBar(jmb);
    }

    //9个按钮 9宫格实现落子操作
    private void addButton(){
        JPanel panel = new JPanel(new GridLayout(3,3));

        for (int i = 0; i < 9; i++) {
            jb[i] = new JButton("");
            //为每一个按钮添加事件监听，见ButtonController类
            jb[i].addActionListener(buttonController);
            panel.add(jb[i]);
        }

        add(panel,BorderLayout.CENTER);
    }

    public JButton[] getJb() {
        return jb;
    }

    public JMenuItem getAbout() {
        return about;
    }

    public JMenuItem getQuit() {
        return quit;
    }

    public JMenuItem getAfresh() {
        return afresh;
    }
}
